package com.yanjue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanjue.bo.UserBookshelfBO;
import com.yanjue.common.entity.UserBookshelf;
import com.yanjue.common.util.Result;
import com.yanjue.common.util.ResultUtil;
import com.yanjue.mapper.UserBookshelfMapper;
import com.yanjue.service.UserBookshelfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户书架控制器
 */
@RestController
@Api("用户书架接口")
@RequestMapping("account/bookshelf")
public class UserBookshelfController {


    private final UserBookshelfService bookshelfService;

    @Autowired
    public UserBookshelfController(UserBookshelfService userBookshelfService) {
        this.bookshelfService = userBookshelfService;
    }

    @Autowired
    private UserBookshelfMapper userBookshelfMapper;

    @ApiOperation(value = "获取书架图书接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "userId", value = "用户ID", required = true, dataType = "int")
    })
    @GetMapping("/get-books")
    public Result getUserBookshelf(@RequestHeader("userId") Integer userId) {
       // UserBookshelf userBookshelf = this.userBookshelfMapper.selectById(userId);
        return this.bookshelfService.getUserBookshelf(userId);
    }



    @ApiOperation(value = "同步书架图书接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "userId", value = "用户ID", required = true, dataType = "int")
    })
    @PostMapping("/sync-book")
    public Result syncUserBookshelf(@RequestHeader("userId") Integer userId, @RequestBody UserBookshelfBO bookshelfBO) {
        return this.bookshelfService.syncUserBookshelf(userId, bookshelfBO);
    }



    @ApiOperation(value = "用户书架是否存在图书", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "userId", value = "用户ID", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", required = true, dataType = "String")
    })
    @GetMapping("/exist-book")
    public Result userBookshelfExistBook(@RequestHeader("userId") Integer userId, String bookId) {
        LambdaQueryWrapper<UserBookshelf> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserBookshelf::getUserId,userId)
                .eq(UserBookshelf::getBookId,bookId);
        Long l = this.userBookshelfMapper.selectCount(lambdaQueryWrapper);
        return ResultUtil.success(l);
    }

}
